package com.situ.company.web220111.score.servlet;

import com.situ.company.web220111.score.model.ScoreModel;
import com.situ.company.web220111.score.service.IScoreService;
import com.situ.company.web220111.score.service.impl.ScoreServiceImpl;
import com.situ.company.web220111.util.FmtRequest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/ScoreServlet")
public class ScoreServlet extends HttpServlet {
    {
        System.out.println(555555);
    }
    private static final long serialVersionUID = 1L;
    private IScoreService service = new ScoreServiceImpl();
    {
        System.out.println(666666);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object res = null;
        System.out.println(req.getParameter("action"));
        switch (req.getParameter("action")) {
            case "page":
                res = page(req);
                break;
            case "del":
                res = del(req);
                break;
            case "get":
                res = get(req);
                break;
            case "upd":
                res = upd(req);
                break;
            case "add":
                res = add(req);
                break;
        }
        FmtRequest.write(resp.getWriter(), res);
    }

    private String add(HttpServletRequest req) {
        ScoreModel scoreModel = new ScoreModel();
        String codeemp = req.getParameter("name");
        String codepro = req.getParameter("prname");
        String score = req.getParameter("score");
        scoreModel.setCodeEmp(codeemp);
        scoreModel.setCodePro(codepro);
        scoreModel.setScore(score);
        return service.insert(scoreModel);

    }

    private String upd(HttpServletRequest req) {
        return service.updateActive(FmtRequest.parseModel(req, ScoreModel.class)) + "";
    }

    private ScoreModel get(HttpServletRequest req) {
        return service.selectModel(FmtRequest.parseModel(req, ScoreModel.class));
    }

    private String del(HttpServletRequest req) {
        return service.delete(FmtRequest.parseModel(req, ScoreModel.class)) + "";
    }

    private Object page(HttpServletRequest req) {
        String codeemp = req.getParameter("name");
        String codepro = req.getParameter("prname");
        String pageIndex = req.getParameter("pageIndex");
        String pageLimit = req.getParameter("pageLimit");
        ScoreModel model = new ScoreModel();
        model.setCodeEmp(codeemp);
        model.setCodePro(codepro);
        model.setPageIndex(Integer.parseInt(pageIndex));
        model.setPageLimit(Integer.parseInt(pageLimit));
        model.setPageOn(true);
        List<ScoreModel> list = service.selectList(model);
        Integer count = service.selectCount(model);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", count);
        return map;
    }

}
